package com.kevin.redisson.controller;

import com.kevin.redisson.configuration.Snowflake;
import com.kevin.redisson.service.RedissonQueue;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Tag(name = "任务管理")
@RestController
@AllArgsConstructor
@RequestMapping("/task")
public class RedissonController {

    @Resource
    private RedissonQueue redissonQueue;

    @Resource
    private Snowflake snowflake;

    @Operation(summary = "添加任务", description = "添加任务")
    @PostMapping
    public ResponseEntity<?> add(@RequestParam(value = "taskId", required = false) String taskId,
                                 @RequestParam(value = "timeout", required = false) Integer timeout) {
        taskId = StringUtils.isEmpty(taskId) ? String.valueOf(snowflake.nextId()) : taskId;
        redissonQueue.offer(taskId, timeout);
        return ResponseEntity.ok().body(redissonQueue.delayedQueue());
    }

    @Operation(summary = "移除任务", description = "移除任务")
    @DeleteMapping(value = "/{taskId}")
    public ResponseEntity<?> remove(@PathVariable("taskId") String taskId) {
        redissonQueue.remove(taskId);
        return ResponseEntity.ok().body(redissonQueue.delayedQueue());
    }

}